#ifndef __INCcliManagerh
#define __INCcliManagerh

#include <cstdio>
#include <map>
#include <vector>
#include <string>
#include <cliArgParse.h>
#include <clibase.h>
#include <SessionManager.h>

namespace cli {
    class cliManager : public objbase
    {
    private:
        class CmdDesc
            {
            public:
                const cli::CliCmd * pCmdDesc;
                std::shared_ptr<Interface> pCmdInst;

            public:
                CmdDesc(const cli::CliCmd * cmdDesc, const char* name, const char* description)
                {
                    pCmdDesc = cmdDesc;
                    pCmdInst = std::make_shared<Interface>(name, description);
                }
                ~CmdDesc()
                {
                    pCmdDesc= nullptr;
                    pCmdInst = nullptr;
                }
            };
    public:
        using CmdDescPtr = std::shared_ptr<CmdDesc>;
        DECLARE_CTOR_DTOR(cliManager);	
        OBJBASE_EXTERN_MEMBER_IMPL();
        STATUS dispatch(SessionPtr session);
        STATUS cliCmdRegister(const cli::CliCmd*, size_t);
        STATUS cliDelCmd(std::string& cmd);
        STATUS cliSetDispatchHook(FUNCPTR callback);

    private:
        PROCESS_DESC(sysShutdown);
        STATUS dispatchInternal(SessionPtr session);
        STATUS dispatchInternal(std::string& cmdStr, SessionPtr session);
        STATUS parse(const std::string& cmdLine, std::string& cmdName, std::vector<std::string>& params);
        CmdDescPtr cliGetCmd(std::string& cmd);
        friend STATUS clibase::printHelp(TextTable &outMsg);

    private:
        DISABLE_COPY_CLASS(cliManager);
        FUNCPTR m_dispatchCallback;
        std::map<std::string, CmdDescPtr> m_cmdList;
    };
}

#endif // __INCcliManagerh
